package view.Professeurs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.Fenetre;
import Controller.ExerciceController;
import Controller.ProfesseurController;
import Model.Exercice;

@SuppressWarnings("serial")
public class ExosAddPanel extends JPanel implements ActionListener {

	private JComboBox listType, listTheme;
	private ArrayList<String> type, theme;
	private JLabel lNomExo;
	private JTextField tNomExo;
	private JButton bValid;

	public ExosAddPanel() {

		// Agencement des conteneurs
		GridBagConstraints constraint = new GridBagConstraints();
		this.setLayout(new GridBagLayout());

		// Ajout de la transparence
		this.setOpaque(false);

		// Creation des conteneurs
		listType = new JComboBox();
		listTheme = new JComboBox();
		type = new ArrayList<String>();
		theme = new ArrayList<String>();


		ExerciceController exc = Fenetre.control.getExerciceController();
		type = exc.listeTypeExercice();

		for (int i=0; i<type.size(); i++) {

			listType.addItem(type.get(i));

		}

		listType.setSelectedIndex(0);
		listType.addActionListener(this);		

		theme = exc.listeTheme();

		for (int i=0; i<theme.size(); i++) {
			listTheme.addItem(theme.get(i));
		}

		listTheme.setSelectedIndex(0);
		listTheme.addActionListener(this);

		lNomExo = new JLabel("Nom de l'exercice : ");
		tNomExo = new JTextField(20);
		tNomExo.addActionListener(this);

		bValid = new JButton("Valider");
		bValid.addActionListener(this);


		//CONTRAINTE DE LNOM

		constraint.gridx = 1;
		constraint.gridy = 0;

		constraint.weightx = 1.0;
		constraint.weighty = 0.1;

		constraint.fill = GridBagConstraints.CENTER;

		constraint.gridwidth = 3;
		constraint.gridheight = 1;

		this.add(lNomExo, constraint);

		//CONTRAINTE DE TNOM

		constraint.gridy = 1;

		this.add(tNomExo, constraint);

		//CONTRAINTE DE LISTTYPE

		constraint.gridy = 2;

		constraint.weightx = 1/3;
		constraint.weighty = 0.8;

		constraint.gridwidth = 1;
		constraint.gridheight = 8;

		this.add(listType, constraint);

		//CONTRAINTE DE LISTTHEME

		constraint.gridx = 2;

		this.add(listTheme, constraint);

		//CONTRAINTE DU BOUTON VALIDER

		constraint.gridx = 3;

		this.add(bValid, constraint);


		Fenetre.fen.validate();

	}

	@Override
	public void actionPerformed(ActionEvent event) {

		if (event.getSource() == bValid)
		{

			ProfesseurController pr = Fenetre.control.getProfesseurController();
			Exercice ex =  Fenetre.control.getExercice(listType.getSelectedItem().toString(),pr.getProfesseur());
			ex.setTheme(theme.get(listTheme.getSelectedIndex()));
			ex.setTitre(tNomExo.getText());

			ProfPage.center.update(ex.getPanel(Fenetre.control));

		}
	} 


}
